/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.finance.listener;

import com.lxk.event.oms.CancelOrderSuccessEvent;
import com.lxk.event.oms.OrderPaySuccessEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.finance.domain.bo.AccUserAccountBo;
import com.lxk.finance.service.IAccUserAccountService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.enums.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 账户充值成功监听事件
 *
 * @author heyi
 * 2024/7/8
 */
@Component
@Slf4j
public class AccountEventListener {

    @Autowired
    private IAccUserAccountService accUserAccountService;

    /**
     * 账户类增加
     *
     * @param event
     */
    @EventListener
    public void accountCreateEvent(OrderPaySuccessEvent event) {
        BigDecimal actPayPrice = event.getActPayPrice();
        String orderNo = event.getOrderNo();
        String orderName = event.getOrderName();
        Long userId = event.getUserId();
        Integer orderType = event.getOrderType();
        if (!OrderEnums.OrderType.VIRTUAL_TYPE_CODE.contains(orderType)) {
            return;
        }
        if (OrderEnums.OrderType.YU_RECHARGE.getCode().equals(orderType)) {
            accUserAccountService.addAccount(userId, UserType.APP_USER.getUserType(), orderNo, actPayPrice, AccUserAccountBo.AccountType.MONEY, orderName);
        }
    }


    /**
     * 取消订单
     *
     * @param event
     */
    @EventListener
    public void cancelEvent(CancelOrderSuccessEvent event) {
        BigDecimal actPayPrice = event.getActPayPrice();
        String orderNo = event.getOrderNo();
        String orderName = event.getOrderName();
        Long userId = event.getUserId();
        Integer orderType = event.getOrderType();
        OrderEnums.OrderStatus orderStatus = event.getOrderStatus();
        if (!OrderEnums.OrderType.VIRTUAL_TYPE_CODE.contains(orderType)) {
            return;
        }
        // 余额支付,扣除余额
        if (OrderEnums.OrderStatus.CONFIRM_400 == orderStatus) {
            if (OrderEnums.OrderType.YU_RECHARGE.getCode().equals(orderType)) {
                accUserAccountService.reduceAccount(userId, UserType.APP_USER.getUserType(), orderNo, actPayPrice, AccUserAccountBo.AccountType.MONEY, "取消" + orderName);
            }
        }
    }

}
